package test;

import java.util.ArrayList;
import java.util.List;

import base.FractalInst;
import jm.JMC;
import jm.audio.Instrument;
import jm.music.data.Note;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.music.tools.Mod;
import jm.util.Play;

// crotchet 四分音符
// Phrase 乐节   phrase.add(note)
// Note 音符   Note n1 = new Note(C4, SN); // C4是音调（音高pitch, int类型），SN是rythmValue（double类型）
//                          八度音分成十二等份，每一份为半音，两个半音相当于全音。 例如：Mod.transpose(phrase, 12); 升八度
//                              
// Score 乐谱，包含多个part
// CPhrase C表示Chord，和弦，CPhrase用于创建和声, 包含一组不同pitch的note，可以和phrrase类似使用（与Phrase类的方法名相同）
// Part 声部  part.add(note),  part.add(phrase) 可以包含phrase或note，往往包含多个phrase, 还包含使用的instrument(乐器）,channel(声道）
// dynamic: 声音强度（音量）int类型, [0,127]之间，最弱-->最响
// tempo:  速度，拍子
// 离线音乐：     note-->phrase-->part-->score
// 在线音乐： note-->RTLine-->RTMixer

// 常用乐器：在ProgramChanges中定义：
// PIANO  --- 钢琴（0）
// EPIANO = 4, ELECTRIC_PIANO = 4, ELPIANO = 4, RHODES = 4, 电钢琴
// NYLON_GUITAR = 24, NGUITAR = 24, GUITAR = 24, ACOUSTIC_GUITAR = 24, AC_GUITAR = 24,  尼龙弦吉他（古典吉他）
//STEEL_GUITAR = 25, SGUITAR = 25, 钢弦吉他
//JAZZ_GUITAR = 26, JGUITAR = 26, CLEAN_GUITAR = 27, 爵士吉他
//CGUITAR = 27, ELECTRIC_GUITAR = 27, EL_GUITAR = 27, 电吉他
// VIOLIN -- 小提琴 40
// VIOLA = 41 中音提琴
// CELLO = 42, VIOLIN_CELLO = 42 大提琴
// SAXOPHONE -- 萨克斯 66
// FLUTE ----横笛 73
//  GUNSHOT--枪响 127

//-------以下为“打击乐器”，需放入channel 10中， 9 = MIDI channel 10
//SLAP_BASS = 36, SBASS = 36, SLAP = 36, SLAP_BASS_1 = 36,   脚大鼓？ 贝司
// octave -- 八度音程 （12pitch)

//http://explodingart.com/jmusic/jmtutorial/t1.html
public class MyTest002 implements JMC {

	public static void main(String[] args) {
		Score score = new Score("我的谱曲");
		Part part1 = new Part("第一声部", FLUTE, 0);// GUITAR,CELLO 第二个参数是“乐器”, int类型，[0,127]

		Phrase[] phrases = new Phrase[16];// new Phrase("乐节1");
		for(int i=0; i<phrases.length; i++) phrases[i] = new Phrase("第"+i+"乐节");
//		List<Note> noteList = new ArrayList<>();
//			int[] pitchArray=new int[] {C4,D4,E4,F4,G4,A4,B4,C5};
		// 音高
		int[] pitches1 = { A4, G4, E4, D4, C4 }; int[] pitches1_2 = { A4, A4, G4, E4, D4, C4 };
		int[] pitches2 = { E4, D4, C4, A3, G3 };
		int[] pitches3 = { G3, A3, G3, A3, C4, D4, E4, G4 };
		int[] pitches4 = { A4, G4, E4, D4, C4, D4 }; int[] pitches4_2 = { A4, G4, E4, D4, C4};
		// 节奏
		double[] rhythms1 = { 0.75, 0.25, 0.5, 0.5, 2 }; double[] rhythms1_2 = { 0.5,  0.25, 0.25, 0.5, 0.5, 2 }; 
		double[] rhythms2 = { 0.75, 0.25, 0.5, 0.5, 2 };
		double[] rhythms3 = { 0.75, 0.25, 0.75, 0.25, 0.75, 0.25, 0.5, 0.5 };
		double[] rhythms4 = { 0.75, 0.25, 0.25, 0.25, 0.5, 2 }; double[] rhythms4_2 = { 0.75, 0.25, 0.5, 0.5, 2 };
		int k=0;
		for (int n = 0; n < 4; n++) {
			if(n==0) {
				addPitchesAndRhythms(phrases[k],pitches1,rhythms1);
			}else {
				addPitchesAndRhythms(phrases[k],pitches1_2,rhythms1_2);
			}
			phrases[k].setPan(0.0);

			addPitchesAndRhythms(phrases[k+1],pitches2,rhythms2);
			phrases[k+1].setPan(0.0);
			
			addPitchesAndRhythms(phrases[k+2],pitches3,rhythms3);
			phrases[k+2].setPan(1.0);
//			
			if(n!=3) {
				addPitchesAndRhythms(phrases[k+3],pitches4,rhythms4);
			}else {
				addPitchesAndRhythms(phrases[k+3],pitches4_2,rhythms4_2);
			}
			phrases[k+3].setPan(1.0);
			k+=4;
		}
//			phrase1.setInstrument(n); //换乐器
//		Mod.repeat(phrase1, 2);
		part1.addPhraseList(phrases);

//		}
		part1.setTempo(180);
		score.add(part1);

		Part part2 = part1.copy();
		part2.setChannel(9);
		score.add(part2);

		System.out.println("score=" + score);
		Play.midi(score);

//		Instrument inst = new SawtoothInst(44100);

	}
	
	public static void addPitchesAndRhythms(Phrase phrase, int[] pitches, double[] rhythms ) {
		List<Note> noteList = new ArrayList<>();
		for (int i = 0; i < pitches.length; i++) {
			Note note = new Note(pitches[i], rhythms[i]);
			if(i==0) {
				note.setDynamic(100);
			}else {
				note.setDynamic(85);
			}
			noteList.add(note);
		}
		phrase.addNoteList(noteList.toArray(new Note[0]));
	}

}
